﻿using FlickrApi.Configuration;
using FlickrApi.Models;
using System.Collections.Generic;
using System.Net.Http;

namespace FlickrApi
{
    public class NativeFlickrApiCaller: IFlickrApiCaller
    {
        public IFlickrApiConfiguration configuration { get; set; }
        /*
    /*{ "photosets": { "page": 1, "pages": 1, "perpage": 2, "total": 2, 
    "photoset": [
      { "id": "72157645446769162", "primary": "13785538023", "secret": "b70453b480", "server": "3767", "farm": 4, "photos": 5, "videos": 0, 
        "title": { "_content": "Test" }, 
        "description": { "_content": "Test" }, "needs_interstitial": 0, "visibility_can_see_set": 1, "count_views": 0, "count_comments": 0, "can_comment": 0, "date_create": "1404193788", "date_update": "1404193789" },
      { "id": "72157645446696572", "primary": "13785575505", "secret": "0d3deea70e", "server": "7418", "farm": 8, "photos": 46, "videos": 0, 
        "title": { "_content": "Leavenworth April 2014" }, 
        "description": { "_content": "" }, "needs_interstitial": 0, "visibility_can_see_set": 1, "count_views": 0, "count_comments": 0, "can_comment": 0, "date_create": "1404193293", "date_update": "1404193296" }
    ] }, "stat": "ok" }*/
        //https://api.flickr.com/services/rest/?method=flickr.photosets.getList&api_key=4a2d46fc376f21c669c1a261385661f7&user_id=123022202%40N04&format=json&nojsoncallback=1
 
        public IEnumerable<PhotoSet> GetListOfPhotoSets()
        {
            var qsParams = new Dictionary<string, string>
            {
                {"method", "flickr.photosets.getList"},
                {"api_key", configuration.ApiKey},
                {"user_id", configuration.UserId},
                {"format", "json"},
                {"nojsoncallback", "1"}
            };

            dynamic response = Utility.Utility.GetJson(configuration.BaseFlickrUrl, HttpMethod.Get, qsParams);

            foreach (var photoset in response.photosets.photoset)
            {
                var ps = new PhotoSet
                {
                    Configuration = configuration,
                    Id = photoset.id.Value,
                    PrimaryPhotoId = photoset.primary.Value,
                    Secret = photoset.secret.Value,
                    Server = photoset.server.Value,
                    Farm = photoset.farm.Value,
                    CountPhotos = long.Parse(photoset.photos.Value),
                    CountVideos = photoset.videos.Value,
                    CountComments = long.Parse(photoset.count_comments.Value),
                    CountViews = long.Parse(photoset.count_views.Value),
                    Description = photoset.description._content.Value,
                    Title = photoset.title._content.Value,
                    CreatedDataTime = Utility.Utility.UnixTimeStampToDateTime(photoset.date_create.Value),
                    UpdatedDateTime = Utility.Utility.UnixTimeStampToDateTime(photoset.date_update.Value)
                };
                yield return ps;
            }
        }

        /*https://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=4a2d46fc376f21c669c1a261385661f7&photoset_id=72157645446769162&format=json&nojsoncallback=1
         
         * { "photoset": { "id": "72157645446769162", "primary": "13785538023", "owner": "123022202@N04", "ownername": "baral.payoj", 
    "photo": [
      { "id": "13785538023", "secret": "b70453b480", "server": "3767", "farm": 4, "title": "image", "isprimary": 1 },
      { "id": "13785515425", "secret": "33abf3e630", "server": "5086", "farm": 6, "title": "image", "isprimary": 0 },
      { "id": "13785533393", "secret": "e50718f371", "server": "2919", "farm": 3, "title": "image", "isprimary": 0 },
      { "id": "13785536633", "secret": "595e41cd91", "server": "7426", "farm": 8, "title": "image", "isprimary": 0 },
      { "id": "13785510385", "secret": "d27f1b3666", "server": "5342", "farm": 6, "title": "image", "isprimary": 0 }
    ], "page": 1, "per_page": "500", "perpage": "500", "pages": 1, "total": 5, "title": "Test" }, "stat": "ok" }
         */

        public IEnumerable<Photo> GetListOfPhotosInPhotoSet(string photoSetId)
        {
            var qsParams = new Dictionary<string, string>
            {
                {"method", "flickr.photosets.getPhotos"},
                {"api_key", configuration.ApiKey},
                {"photoset_id", photoSetId},
                {"format", "json"},
                {"nojsoncallback", "1"}
                //TODO: paging - currently it's 500 photos per page
            };

            dynamic response = Utility.Utility.GetJson(configuration.BaseFlickrUrl, HttpMethod.Get, qsParams);

            foreach (var photo in response.photoset.photo)
            {
                var p = new Photo
                {
                    Configuration = configuration,
                    Id = photo.id.Value,
                    Secret = photo.secret.Value,
                    Server = photo.server.Value,
                    Farm = photo.farm.Value,
                    Title = photo.title.Value,
                    IsPrimary = (photo.isprimary == 1)
                };

                yield return p;
            }
        }
    }
}
